import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";
import vueJsx from "@vitejs/plugin-vue-jsx";
import Pages from "vite-plugin-pages";
import devProxy from "./devProxy";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // console.log("cwd: ", process.cwd());
  const env = loadEnv(mode, process.cwd(), "");
  return {
    // define: {
    //   'process.env': {
    //     VITE_APP_BASE_URL: env.VITE_APP_BASE_URL,
    //   }
    // },
    base: "./", // 设置项目的公共基础路径
    server: {
      host: true,
      port: 8010,
      cors: true, // 允许跨域
      // 设置代理，根据项目实际情况配置
      proxy: devProxy,
    },
    build: {
      outDir: "dist",
    },
    plugins: [
      Pages({
        dirs: "src/views", // 路由组件目录
        exclude: ["**/components/*.vue", "**/modules/*.vue"], // 排除组件
      }),
      vue(),
      vueJsx(),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    css: {
      // SCSS 预处理器配置（编译前）
      preprocessorOptions: {
        scss: {
          // api: "modern-compiler",
          implementation: "sass", // 强制使用 Dart Sass
          additionalData: `@use "@/assets/styles/variables.scss" as *;`, // 全局注入
        },
      },
      postcss: { config: true },
    },

    resolve: {
      alias: {
        "@": "/src",
      },
    },
  };
});
